﻿using System;
using WiMo.Games;
using WiMo.Games.Inputs;
using WiMo.Games.Drawables;

namespace Shapes
{
    public class Game : IGame
    {
        private int _state = 0;
        const int MAX_STATES = 4;

        ISpriteShape _rect1;
        ISpriteShape _rect2;
        ISpriteShape _rect3;
        ISpriteShape _rect4;
        ISpriteShape _lineList;
        ISpriteShape _polygon;
        ISpriteShape _textureFilledPolygon;
        ISpriteShape _ellipse1;
        ISpriteShape _ellipse2;
        ISpriteImage _image;
        ISpriteText _spriteText;
        ITexture _background;

        public Game(IGameEngine engine)
        {
            Engine = engine;
            Engine.ContentManager.ContentLocation = "Content";
            Engine.Display.BackgroundColor = Color.CornFlowerBlue;
            Engine.EnableDiagnostics = true;
            Engine.DiagnosticsFontColor = Color.Blue;
        }

        public void Initialize()
        {
        }

        public void BeginRun()
        {
        }

        public void CreateMenus()
        {
            Engine.InputManager.LeftSoftButton = "Next";
            Engine.InputManager.RightSoftButton = "Exit";
            Engine.InputManager.SoftMenuFontColor = Color.White;
            Engine.InputManager.MenuBarBackgroundColor = new Color(Color.Navy, 0x7f);
        }

        public void LoadContent()
        {
            _background = Engine.ContentManager.GetTexture("Background");

            _spriteText = Engine.AssetFactory.CreateText();
            _spriteText.Font = Engine.DefaultFont;
            _spriteText.Origin = Origin.Center;
            _spriteText.Position = new PointF(Engine.Display.TargetSize.Width / 2, 35);
            _spriteText.Text = "Hello WiMoGF";
            _spriteText.ForeColor = Color.White;

            _image = Engine.ContentManager.LoadImage("Ball");
            _image.Position = new PointF(Engine.Display.TargetSize.Width / 2, 55);
            _image.ColorKey = Color.White;

            _lineList = Engine.AssetFactory.CreateLineList();
            _lineList.Origin = Origin.TopLeft;
            _lineList.Position = new PointF(25, 25);
            _lineList.AddPoint(new Location(0, 0));
            _lineList.AddPoint(new Location(130, 50));
            _lineList.AddPoint(new Location(130, 200));
            _lineList.AddPoint(new Location(80, 150));
            _lineList.AddPoint(new Location(15, 20));
            _lineList.BorderColor = Color.White;
            _lineList.BorderWidth = 4;
            _lineList.CalcBoundingRect();
            
            _textureFilledPolygon = Engine.AssetFactory.CreatePolygon();
            _textureFilledPolygon.Position = new PointF(70, 95);
            _textureFilledPolygon.AddPoint(new Location(0, 0));
            _textureFilledPolygon.AddPoint(new Location(120, 5));
            _textureFilledPolygon.AddPoint(new Location(90, 30));
            _textureFilledPolygon.AddPoint(new Location(5, 30));
            _textureFilledPolygon.AddPoint(new Location(0, 0));
            _textureFilledPolygon.BorderColor = Color.White;
            _textureFilledPolygon.BorderWidth = 1;
            _textureFilledPolygon.SetTexture(Engine.ContentManager,"Ground");
            _textureFilledPolygon.CalcBoundingRect();

            _rect1 = Engine.AssetFactory.CreateRectangle();
            _rect1.Position = new PointF(Engine.Display.TargetSize.Width / 2, 140);
            _rect1.Size = new WiMo.Games.Size(120, 60);
            _rect1.BorderColor = Color.Yellow;
            _rect1.FillColor = Color.Red;
            _rect1.BorderWidth = 3;
            _rect1.BorderRadius = 15;

            _rect2 = Engine.AssetFactory.CreateRectangle();
            _rect2.Position = new PointF((Engine.Display.TargetSize.Width / 2) + 20, 160);
            _rect2.Size = new WiMo.Games.Size(120, 50);
            _rect2.BorderColor = Color.Silver;
            _rect2.FillColor = new Color(Color.Tan, 0xcb);
            _rect2.BorderWidth = 5;
     
            _rect3 = Engine.AssetFactory.CreateRectangle();
            _rect3.Position = new PointF((Engine.Display.TargetSize.Width / 2) - 20, 290);
            _rect3.Size = new WiMo.Games.Size(120, 40);
            _rect3.FillColor = Color.Green;
            _rect3.BorderWidth = 5;
            _rect3.BorderRadius = 20;
            _rect3.RenderStrategy = RenderStrategy.Standard;

            _rect4 = Engine.AssetFactory.CreateRectangle();
            _rect4.Position = new PointF((Engine.Display.TargetSize.Width / 2) - 20, 240);
            _rect4.Size = new WiMo.Games.Size(120, 40);
            _rect4.FillColor = Color.Yellow;
            _rect4.BorderColor = Color.Black;
            _rect4.BorderWidth = 5;
            _rect4.BorderRadius = 20;
            _rect4.RenderStrategy = RenderStrategy.Quality;

            _polygon = Engine.AssetFactory.CreatePolygon();
            _polygon.Position = new PointF(70, 155);
            _polygon.AddPoint(new Location(50, 0));
            _polygon.AddPoint(new Location(75, 25));
            _polygon.AddPoint(new Location(100, 25));
            _polygon.AddPoint(new Location(75, 50));
            _polygon.AddPoint(new Location(100, 75));
            _polygon.AddPoint(new Location(50, 50));
            _polygon.AddPoint(new Location(0, 75));
            _polygon.AddPoint(new Location(25, 50));
            _polygon.AddPoint(new Location(0, 25));
            _polygon.AddPoint(new Location(25, 25));
            _polygon.AddPoint(new Location(50, 0));
            _polygon.FillColor = Color.SpringGreen;
            _polygon.BorderColor = Color.Yellow;
            _polygon.BorderWidth = 1;
            _polygon.CalcBoundingRect();            

            _ellipse1 = Engine.AssetFactory.CreateEllipse();
            _ellipse1.Origin = Origin.Center;
            _ellipse1.Position = new PointF(Engine.Display.TargetSize.Width / 2, 250);
            _ellipse1.Size = new WiMo.Games.Size(200, 30);
            _ellipse1.FillColor = Color.Blue;
            _ellipse1.BorderColor = Color.Yellow;
            _ellipse1.BorderWidth = 1;

            _ellipse2 = Engine.AssetFactory.CreateEllipse();
            _ellipse2.Origin = Origin.Center;
            _ellipse2.Position = new PointF(Engine.Display.TargetSize.Width / 2, 200);
            _ellipse2.Size = new WiMo.Games.Size(200, 30);
            _ellipse2.FillColor = Color.Blue;
            _ellipse2.BorderColor = Color.Yellow;
            _ellipse2.BorderWidth = 3;
        }

        private IInputManager _previous;
        public void Update(GameTime gameTime)
        {
            if (Engine.InputManager.IsKeyDown(Keys.Return))
                Engine.Exit();

            if (Engine.InputManager.IsKeyDown(Keys.RightSoftButton))
                Engine.Exit();

            if (Engine.InputManager.WasPressed(_previous, Keys.LeftSoftButton))
            {
                _state++;
                if (_state == MAX_STATES)
                    _state = 0;
            }

            _previous = this.Engine.InputManager.GetKeysState();
        }

        public void Draw(GameTime gameTime)
        {
            Engine.Display.Add(_spriteText);
            Engine.Display.BackgroundImage = _background;
 
            switch (_state)
            {
                case 0:
                    //Engine.Display.Add(_rect1);                    
                    //Engine.Display.Add(_rect2);
                    Engine.Display.Add(_rect3);
                    Engine.Display.Add(_rect4);
                    break;
                case 1:
                    Engine.Display.Add(_textureFilledPolygon);
                    Engine.Display.Add(_polygon);
                    break;
                case 2:
                    Engine.Display.Add(_ellipse1);
                    Engine.Display.Add(_ellipse2);
                    Engine.Display.Add(_lineList);
                    break;
                case 3:
                    Engine.Display.Add(_image);
                    break;
            }
        }

        public bool CanExit()
        {
            return true;
        }

        public IGameEngine Engine { get; set; }
    }
}
